package com.baizhi.controller;

import com.baizhi.annotation.CmfzLogAnnotation;
import com.baizhi.entity.CmfzAdmin;
import com.baizhi.service.CmfzAdminService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.Map;

/**
 * Created by Administrator on 2019/4/20 0020.
 */
@Controller
@RequestMapping("/admin")
public class CmfzAdminController {
    @Autowired
    private CmfzAdminService cmfzAdminService;
    private Logger logger= LoggerFactory.getLogger("CmfzAdminController.class");


    @RequestMapping("/regist")
    public String regist(String adminUsername, String adminPassword){
        Boolean regist = cmfzAdminService.regist(adminUsername,adminPassword );
        if(regist){
            return "redirect:/login.jsp";
        }else {
            return "redirect:/regist.jsp";
        }
    }


    @CmfzLogAnnotation("管理员登录！")
    @RequestMapping("/login")
    public String login(String adminUsername, String adminPassword, String code, HttpSession session) {
        CmfzAdmin cmfzAdmin = cmfzAdminService.login(adminUsername, adminPassword);
        if (cmfzAdmin != null) {
            session.setAttribute("cmfzAdmin", cmfzAdmin);
            return "redirect:/main/main.jsp";
        } else {
            return "redirect:/login.jsp";
        }
    }

    @RequestMapping("/logout")
    public String logout(HttpSession session) {
        cmfzAdminService.logout(session);
        return "redirect:/login.jsp";
    }

    @RequestMapping("/showAll")
    public @ResponseBody Map showAll(Integer page, Integer rows){
        Map map = cmfzAdminService.queryAll(page, rows);
        return map;
    }


}
